package whu.o2o;

import java.io.Serializable;
import java.util.ArrayList;

import whu.o2o.View.LoadDialog;
import whu.o2o.adapter.RouteAdapter;
import whu.o2o.util.ConstantData;
import whu.o2o.util.ParseXml;
import whu.o2o.vo.RouteVO;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import whu.o2o.R;
import whu.o2o.connection.CommonHttpConnect;
import whu.o2o.connection.Params;





public class RouteListActivity extends BaseActivity implements OnItemClickListener, OnScrollListener {
	private ListView mListView;
	private RouteAdapter mAdapter;
	private ArrayList<RouteVO> mRouteList;
	private String mTitle;
	private String mUrl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_common_list);
		getIntentData();
		initObject();
		initView();
		initListener();
		//initData();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		mListViewLastItem = firstVisibleItem + visibleItemCount - 1;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if (mListViewLastItem == mRouteList.size() - 1) {
			RouteVO routeVO = mRouteList.get(mRouteList.size() - 1);
			if (routeVO != null && routeVO.getPage() != -1) {
				requestData(routeVO.getPage());
			}
		}
	}

	@Override
	protected void parseData(final String xml) {
		mApplication.getThreadPool().execute(new Runnable() {

			@Override
			public void run() {
				ParseXml.getInstance().parseRouteXml(xml, mRouteList);
				mHttpListHandler.sendEmptyMessage(ConstantData.Data.PARSE_XML_MSG_CODE);
			}
		});

	}

	@Override
	protected void setAdapter() {
		if (mAdapter == null) {
			mAdapter = new RouteAdapter(this, mRouteList);
			mListView.setAdapter(mAdapter);
		} else {
			mAdapter.notifyDataSetChanged();
		}

	}

	@SuppressWarnings("unchecked")
	private void getIntentData() {
		Intent intent = getIntent();
		if (intent != null) {
			mTitle = intent.getStringExtra("title");
			mUrl = intent.getStringExtra("url");

			Serializable serializable = intent.getSerializableExtra("route");
			if (serializable != null) {
				mRouteList = (ArrayList<RouteVO>) serializable;
			}
		}
	}

	private void initObject() {
		if (mRouteList == null) {
			mRouteList = new ArrayList<RouteVO>();
		}

	}

	private void initView() {
		if (!TextUtils.isEmpty(mTitle)) {
			setTitle(mTitle);
		} else {
			setTitle("搜索结果");
		}

		mListView = (ListView) findViewById(R.id.listView);
	}

	private void initListener() {
		backBtnClick();
		mListView.setOnItemClickListener(this);
		mListView.setOnScrollListener(this);
	}

	private void initData() {
		if (!TextUtils.isEmpty(mUrl) && mRouteList.size() == 0) {
			requestData(BaseActivity.FIRST_PAGE);
		} else {
			setAdapter();
		}
	}

	private void requestData(int page) {
		Params params = new Params();
		params.setUrl(mUrl + page);
		params.setHandler(mHttpListHandler);
		params.setRequestType(ConstantData.Data.GET_HTTP);
		params.setMsgCode(ConstantData.Data.GET_HTTP_RESULT_MSG_CODE);
		params.setActivity(this);
		params.setShowLoad(true);
		LoadDialog.showDailog(this);
		mApplication.getThreadPool().execute(new CommonHttpConnect(params));
	}

}
